﻿using PLCS.Application.Contracts.EquipmentApp.Dtos;
using PLCS.Client.IService.EquipmentManager;

using Volo.Abp.Application.Dtos;

namespace PLCS.Client.Service.EquipmentManager;

public class EquipmentService : BaseService<EquipmentDto, EquipmentEditDto>, IEquipmentService
{
    public async Task CreateEquipmentListAsync(IEnumerable<EquipmentEditDto> equipments)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"equipment/list", parameter: equipments);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<EquipmentDto> GetByEquipmentNoAsync(string equipmentNo)
    {
        RequestParam requestParam = new RequestParam(method: Method.Get, route: $"equipment/bySNo?no={equipmentNo}");
        var apiRes = await restClient.ExecuteAsync<EquipmentDto>(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
        else
            return apiRes.Data;
    }

    public async Task DeleteAllAsync()
    {
        RequestParam requestParam = new(method: Method.Delete, route: $"equipment/all");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<PagedResultDto<EquipmentDto>> GetListBySearchAsync(int skipCount, int maxResultCount, string? equipmentNo, string? equipmentType)
    {
        var route = $"equipment/bySearch?SkipCount={skipCount}&MaxResultCount={maxResultCount}";
        if (!string.IsNullOrEmpty(equipmentNo))
            route += $"&equipmentNo={equipmentNo}";
        if (!string.IsNullOrEmpty(equipmentType))
            route += $"&equipmentType={equipmentType}";

        RequestParam requestParam = new(method: Method.Get, route: route);
        var apiRes = await restClient.ExecuteAsync<PagedResultDto<EquipmentDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return new PagedResultDto<EquipmentDto>();
    }
}